//
//  CollectionViewController.m
//  PaiLeXiongProject
//
//  Created by 小獾科技 on 2017/9/11.
//  Copyright © 2017年 小獾科技. All rights reserved.
//

#import "CollectionViewController.h"
#import "GoodsTableViewCell.h"
#import "CollectionGoodsModel.h"
@interface CollectionViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *datasource;//商品数组
@property (nonatomic, strong) LoadingView *load;
@end

@implementation CollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"商品收藏";
    [self addBackItem];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, GWIDTH, GHEIGHT - 64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorColor = ColorClear;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[GoodsTableViewCell class] forCellReuseIdentifier:@"goods"];

    
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(deRefresh)];
    [_tableView.mj_header beginRefreshing];
    _load = [[LoadingView alloc] initWithFrame:CGRectMake(0, 0, GWIDTH, GHEIGHT - 64)];
    [_load setReminderLabelTitle:@"暂无收藏~" andImage:@"collect_empty"];
    _load.hidden = YES;
    [_load.refreshBtn addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_load];
    [self.view bringSubviewToFront:_load];
    
}

- (void)deRefresh
{
    [self startNetwork:@"1"];//网络请求 默认是 商品
}

- (void)handleRefresh:(UIButton *)sender
{
    [self deRefresh];
}

- (void)startNetwork:(NSString *)str
{
    _datasource = [NSMutableArray arrayWithCapacity:0];
    [_datasource removeAllObjects];
    NSDictionary *param = @{@"type": str};
    @weakobj(self);
    [NetWork sendGetRequestURL:[NSString stringWithFormat:@"%@/collect/GetCollect?type=%@", GW_Domain, str] parameters:param completionBlockWithSuccess:^(NSDictionary *resultDict) {
        NSLog(@"%@", resultDict);
        @strongobj(self);
        if([str isEqualToString:@"1"])//商品
        {
            for (NSDictionary *dic in resultDict[@"data"]) {
                CollectionGoodsModel *model = [GW_Tool getObjFromDictionary:dic byClassName:@"CollectionGoodsModel"];
                [self.datasource addObject:model];
            }
        }
        [_tableView.mj_header endRefreshing];
        [_tableView reloadData];
        
        if(self.datasource.count > 0)
        {
            _load.hidden = YES;
        }
        else
        {
            _load.hidden = NO;
        }
        MESSAGE_TOAST(resultDict[@"message"]);
    } andFailure:^(NSError *error, NSString *errorMsg) {
        NSLog(@"%@", error);
        _load.hidden = NO;
    }];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 106;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datasource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    CollectionGoodsModel *model = self.datasource[indexPath.row];
    GoodsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"goods"];
    [cell configViewWithModel:model];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *lat = [[NSUserDefaults standardUserDefaults] objectForKey:@"lat"];
    NSString *lon = [[NSUserDefaults standardUserDefaults] objectForKey:@"long"];
    CollectionGoodsModel *model = self.datasource[indexPath.row];
    [GW_webView handleWebView:[NSString stringWithFormat:@"%@/product/productDel.html?gid=%@&lat=%@&lon=%@", GW_Domain, model.id, lat, lon] viewController:self titleName:@"首页" goodid:model.id];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
